<template>
  <div>
    <hr />
    <textarea
      placeholder="请输入要BB的内容(最多120字)"
      maxlength="120"
      v-model="msg"
    ></textarea>
    <mt-button type="primary" size="large" @click="postMsg">发表评论</mt-button>

    <!-- 评论列表 -->
    <div class="cmt-list">
      <div class="cmt-item" v-for="(item, i) in cmtlist" :key="i">
        <div class="cmt-item-title">
          第{{ i + 1 }}楼&nbsp; 用户：{{ item.user_name }}&nbsp; 发表时间：{{
            item.add_time | dateFormat
          }}
        </div>
        <div class="cmt-item-body">{{ item.content }}</div>
      </div>
    </div>

    <mt-button type="danger" size="large" plain @click="loadMore"
      >加载更多</mt-button
    >
  </div>
</template>

<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      page: 1, //默认展示第一页的评论
      cmtlist: [], //评论数组
      msg: "", //即将发表的内容
    };
  },
  created() {
    this.getCommentByPage();
  },
  methods: {
    async getCommentByPage() {
      //根据页数来获取评论数据
      const { data } = await this.$http.get(
        "/api/getcomments/" + this.newsid + "?pageindex=" + this.page
      );
      console.log(data);
      if (data.status === 0)
        return (this.cmtlist = this.cmtlist.concat(data.message));
    },
    loadMore() {
      this.page++;
      this.getCommentByPage();
    },
    async postMsg() {
      //点击发表评论
      if (this.msg.trim().length <= 0) return Toast("请填写内容!!");
      const { data } = await this.$http.post(
        "/api/postcomment/" + this.newsid,
        { content: this.msg.trim() }
      );
      if (data.status === 0) {
        //刷新评论列表
        // this.cmtlist = [];
        // this.getCommentByPage();

        // 为了防止重新调用getCommentByPage方法的时候，会把之前所有的评论清空的问题
        // 我们在客户端，手动拼接出一个评论的对象，并把这个评论的对象，unshift到cmtlist中
        this.cmtlist.unshift({
          user_name: "匿名用户",
          add_time: new Date(),
          content: this.msg.trim(),
        });
      }
    },
  },
  props: ["newsid"], //接受父组件向子组件传来的id
};
</script>

<style lang="scss" scoped>
textarea {
  font-size: 14px;
  margin: 0;
}
.cmt-list {
  margin-top: 4px;
  .cmt-item {
    line-height: 30px;
    .cmt-item-title {
      font-size: 14px;
      background-color: #ddd;
    }
    .cmt-item-body {
      font-size: 13px;
      text-indent: 2em;
    }
  }
}
</style>